package com.joshondesign.sdljava.gfx;

import com.joshondesign.sdljava.SDL_Event;
import com.joshondesign.sdljava.SDL_EventType;

/**
 * Created by IntelliJ IDEA.
 * User: josh
 * Date: 9/9/11
 * Time: 4:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class Trigger {
    public static int LeftArrow = 123;
    public static int RightArrow = 124;

    public static TriggerRule KeyPressed(int key) {
        return new KeyTriggerRule(key);
    }

    public static TriggerRule MouseDragged() {
        return new MouseTriggerRule();
    }

    public static TriggerRule MousePressed() {
        return new MouseTriggerRule();
    }

    public static TriggerRule MouseReleased() {
        return new MouseTriggerRule();
    }

    public static abstract class TriggerRule {
        private boolean repeat;
        protected Callback callback;


        public TriggerRule setRepeat(boolean b) {
            this.repeat = b;
            return this;
        }

        public TriggerRule setCallback(Callback callback) {
            this.callback = callback;
            return this;
        }

        public void call(SDL_Event event) {
            this.callback.call(null);
        }

        public abstract boolean matches(SDL_Event event);
    }

    private static class MouseTriggerRule extends TriggerRule {
        @Override
        public boolean matches(SDL_Event event) {
            if(event.getType() == SDL_EventType.SDL_MOUSEBUTTONDOWN) {
                return true;
            }
            if(event.getType() == SDL_EventType.SDL_MOUSEMOTION) {
                return true;
            }
            if(event.getType() == SDL_EventType.SDL_MOUSEBUTTONUP) {
                return true;
            }
            return false;
        }
        public void call(SDL_Event event) {
            this.callback.call(event);
        }
    }

    private static class KeyTriggerRule extends TriggerRule {
        private int key;
        public KeyTriggerRule(int key) {
            super();
            this.key = key;
        }
        public boolean matches(SDL_Event event) {
            if(event.getType() == SDL_EventType.SDL_KEYDOWN) {
                if(event.getKey().getKeysym().getScancode() == this.key) {
                    return true;
                }
            }
            return false;
        }
    }
}
